<?php

class Model
{
	/**
	 * @var array
	 */
	private static $_models = array();

	/**
	 * @var array
	 */
	private static $_tables = array();
	/**
	 * @var string
	 */
	private static $_prefix = 'agilet_';

	/**
	 * @property
	 */
	static public function getTable($name)
	{
		if (isset(self::$_tables[$name]))
		{
			return self::$_tables[$name];
		}

		$tableClass = Agilet::sanitizeClass($name, 'model/table');
		
		$rowClass = Agilet::sanitizeClass($name, 'model');
		
		return self::$_tables[$name] = new $tableClass(array('rowClass'=>$rowClass));
	}

	/**
	 * @return Database_Table_Row
	 */
	static public function factory($name, $key = null)
	{
		if (isset(self::$_models[$name]))
		{
			$cls = self::$_models[$name];
		}
		else
		{
			self::$_models[$name] = $cls = Agilet::sanitizeClass($name, 'model');
		}

		if ($key)
		{
			$obj = new $cls;
			return $obj -> getTable() -> find($key) -> current();
		}
		return new $cls;
	}

	static public function getTableName($name)
	{
		return self::$_prefix . $name;
	}

}
